import { getTimeDuration, deepClone } from '../common/utils';
import { WXPAYMENT_ORGANIZATION_TYPE_VALUE, WXPAYMENT_CONTACT_TYPE_VALUE } from '../constants/payment';

/**
 * 处理管理员列表数据
 * @param {Array} list 数据列表
 */
export const dealAdminListData = (list) => {
    return list.map(item => {
        return {
            id: item.brandAdminId,
            name: item.userNickName,
            nickName: item.aliasName,
            avatar: item.userPic,
            desc: item.isBrandOwner ? '创建者' : '管理员',
        };
    });
};

/**
 * 处理分销员列表数据
 * @param {Array} list 数据列表
 */
export const dealDistributorListData = (list) => {
    return list.map(item => {
        return {
            id: item.brandDistributorId,
            name: item.distributorUserNickName,
            nickName: item.aliasName,
            avatar: item.distributorUserPic,
            desc: `${getTimeDuration(item.recentNews, '前')}查看`,
            fansCount: item.fansCount,
            activityCount: item.distributeActivityCount,
            saleAmount: `￥${item.saleAmount / 100}`,
        };
    });
};

/**
 * 处理分销员申请审核列表数据
 * @param {Array} list 数据列表
 */
export const dealDistributorRequestListData = (list) => {
    return list.map(item => {
        const obj = item.applyFields ? JSON.parse(item.applyFields) : {};
        const name = obj.nick_name;
        const fieldList = [];
        obj.mobile && fieldList.push({ value: 'mobile', name: obj.mobile });
        obj.wechat_id && fieldList.push({ value: 'wechat_id', name: obj.wechat_id });
        obj.remark && fieldList.push({ value: 'remark', name: obj.remark });
        return {
            id: item.brandDistributeRequestId,
            name,
            avatar: item.userPic,
            date: `${getTimeDuration(item.requested, '前')}`,
            auditResult: item.auditResult,
            fieldList,
        };
    });
};

/**
 * 处理代言人列表数据
 * @param {Array} list 数据列表
 */
export const dealSpokesmanListData = (list) => {
    return list.map(item => {
        return {
            id: item.brandSpokesmanId,
            name: item.spokesmanUserNickName,
            nickName: item.aliasName,
            avatar: item.spokesmanUserPic,
            desc: `${getTimeDuration(item.recentNews, '前')}查看`,
            fansCount: item.fansCount,
            activityCount: item.endorseActivityCount,
            saleAmount: `￥${item.saleAmount / 100}`,
        };
    });
};

/**
 * 处理代言人申请审核列表数据
 * @param {Array} list 数据列表
 */
export const dealSpokesmanRequestListData = (list) => {
    return list.map(item => {
        const obj = item.applyFields ? JSON.parse(item.applyFields) : {};
        const name = obj.nick_name;
        const fieldList = [];
        obj.mobile && fieldList.push({ value: 'mobile', name: obj.mobile });
        obj.wechat_id && fieldList.push({ value: 'wechat_id', name: obj.wechat_id });
        obj.remark && fieldList.push({ value: 'remark', name: obj.remark });
        return {
            id: item.brandEndorseRequestId,
            name,
            avatar: item.userPic,
            date: `${getTimeDuration(item.requested, '前')}`,
            auditResult: item.auditResult,
            fieldList,
        };
    });
};

/**
 * 处理粉丝列表数据
 * @param {Array} list 数据列表
 */
export const dealFansListData = (list) => {
    return list.map(item => {
        return {
            id: item.brandFanId,
            name: item.followerUserNickName,
            nickName: item.aliasName,
            avatar: item.followerUserPic,
            desc: `${getTimeDuration(item.recentNews, '前')}查看`,
            viewCount: item.viewCount,
            activityCount: item.involveActivityCount,
            payAmount: `￥${item.payAmount / 100}`,
        };
    });
};

/**
 * 处理仓库列表数据
 * @param {Array} list 数据列表
 */
export const dealWarehouseListData = (list) => {
    return list.map(item => {
        return {
            id: item.brandWarehouseId,
            name: item.name,
            address: item.address,
            linkman: item.linkman,
            tel: item.tel,
            deliveryModes: item.deliveryModes,
        };
    });
};

/**
 * 处理默认微信支付申请信息
 */
export const dealWxpayPaymentDefaultData = () => {
    return {
        needAccountInfo: false,
        applyType: 'ecommerce',
        organizationType: '',
        idDocType: 'IDENTIFICATION_TYPE_MAINLAND_IDCARD',
        idCardInfo: {},
        salesSceneInfo: {},
        contactInfo: {
            contactType: WXPAYMENT_CONTACT_TYPE_VALUE.owner,
        },
    };
};

/**
 * 处理微信支付申请保存信息
 */
export const dealWxpayPaymentSaveData = (formData) => {
    formData = deepClone(formData);
    const { organizationType, idCardInfo, bizLicenseInfo } = formData;
    bizLicenseInfo && delete formData.bizLicenseInfo.copyUrl;
    idCardInfo && delete formData.idCardInfo.cardUrl;
    idCardInfo && delete formData.idCardInfo.nationalUrl;
    if (organizationType === WXPAYMENT_ORGANIZATION_TYPE_VALUE.micro) {
        delete formData.qualifications;
        delete formData.qualificationsUrl;
        delete formData.bizLicenseInfo;
        formData.contactInfo.contactType = WXPAYMENT_CONTACT_TYPE_VALUE.owner;
    }
    if (formData.contactInfo.contactType = WXPAYMENT_CONTACT_TYPE_VALUE.owner) {
        formData.contactInfo.contactName = idCardInfo.idCardName;
        formData.contactInfo.contactIdCardNumber = idCardInfo.idCardNumber;
    }
    return formData;
};
